﻿using Entity.IRepository.Project;
using Entity.ProjectEnt;
using Logic.Repository.Project;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ZFine.Code;

namespace ZFine.Application.Project
{
    public class CarriageBoxApp
    {
        private ICarriageBoxRepository service = new CarriageBoxRepository();
        public List<CarriageBoxEntity> GetList()
        {
            var expression = ExtLinq.True<CarriageBoxEntity>();
            return service.IQueryable(expression).ToList();
        }

        public List<CarriageBoxEntity> GetList(string sql) {
            return service.FindList(sql);
        }
        public List<CarriageBoxEntity> GetList(Expression<Func<CarriageBoxEntity, bool>> predicate)
        {
            return service.IQueryable(predicate).ToList();
        }
        public List<CarriageBoxEntity> GetList(Pagination pagination, string keyword, string StartTime, string EndTime, string Daozhan, string Zhuangche, string Xiangzhu)
        {
            var expression = ExtLinq.True<CarriageBoxEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(f => f.CarriageBoxID.Contains(keyword));
            }
            if (!string.IsNullOrEmpty(Daozhan))
            {
                expression = expression.And(f => f.InitBelongCarriageId.Contains(Daozhan));
            }
            if (!string.IsNullOrEmpty(Zhuangche))
            {
                expression = expression.And(f => f.ChangeBelongCarriageId.Contains(Zhuangche));
            }
            if (!string.IsNullOrEmpty(Xiangzhu))
            {
                expression = expression.And(f => f.BoxType.Equals(Xiangzhu));
            }
            if (!string.IsNullOrEmpty(StartTime))
            {
                DateTime sTime = Convert.ToDateTime(StartTime);
                if (!string.IsNullOrEmpty(EndTime))
                {
                    DateTime eTime = Convert.ToDateTime(EndTime);
                    expression = expression.And(f => f.InitBelongTime >= sTime);
                    expression = expression.And(f => f.InitBelongTime < eTime);
                }
                else
                {
                    expression = expression.And(f => f.InitBelongTime >= sTime);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(EndTime))
                {
                    DateTime eTime = Convert.ToDateTime(EndTime);
                    expression = expression.And(f => f.InitBelongTime < eTime);
                }
            }
            return service.FindList(expression, pagination);
        }

        public List<CarriageBoxEntity> GetEmptyCarBoxList(Pagination pagination2)
        {
            var expression = ExtLinq.True<CarriageBoxEntity>();
            expression = expression.And(f => f.ChangeBelongCarriageId == null && f.ChangeBelongTime == null);
            return service.FindList(expression, pagination2);
        }

        public List<CarriageBoxEntity> GetListArrive(Pagination pagination, string keyword, string StartTime, string EndTime, string carId, string customerName)
        {
            var expression = ExtLinq.True<CarriageBoxEntity>();
            //if (!string.IsNullOrEmpty(keyword))
            //{
            //    expression = expression.And(f => f.PathWay.Contains(keyword));
            //}
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(f => f.ThingsName.Contains(keyword));
            }


            //expression = expression.And(t => t.State == true);
            return service.FindList(expression, pagination);
        }

        public List<CarriageBoxEntity> GetListByCarriageCarCode(string keyValue, Pagination pagination)
        {
            var expression = ExtLinq.True<CarriageBoxEntity>();
            expression = expression.And(f => f.InitBelongCarriageId == keyValue);
            return service.FindList(expression, pagination);
        }

        public List<CarriageBoxEntity> GetListByCarriageChangeCarCode(string keyValue, Pagination pagination)
        {
            var expression = ExtLinq.True<CarriageBoxEntity>();
            expression = expression.And(f => f.ChangeBelongCarriageId == keyValue);
            return service.FindList(expression, pagination);
        }

        public void DeleteFormByCarcode(string carCode)
        {
            var expression = ExtLinq.True<CarriageBoxEntity>();
            expression = expression.And(t => t.CarriageBoxID == carCode);
            service.Delete(expression);
        }

        public List<CarriageBoxEntity> GetListD(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<CarriageBoxEntity>();
            return service.FindList(expression, pagination);
        }
        public CarriageBoxEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public int DeleteForm(string keyValue)
        {
            var local = service.FindEntity(keyValue);
            if (local != null)
            {
                return service.Delete(local);
            }
            else
            {
                return -1;
            }
        }

        public CarriageBoxEntity GetEntityByCarriageBoxId(string carriageBoxID)
        {
            var expression = ExtLinq.True<CarriageBoxEntity>();
            expression = expression.And(f => f.CarriageBoxID == carriageBoxID);
            return service.FindEntity(expression);
        }

        public int SubmitForm(CarriageBoxEntity ServiceEntity, string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (LoginInfo != null)
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    ServiceEntity.Modify(keyValue);
                    return service.Update(ServiceEntity);
                }
                else
                {
                    ServiceEntity.Create();
                    return service.Insert(ServiceEntity);
                }
            }
            return -1;
        }

        public int SubmitSaveList(List<CarriageBoxEntity> insertObjList)
        {
           return service.UpdateList(insertObjList);
        }

        public int SubmitAddCarBox(List<CarriageBoxEntity> objlist, ref string msg)
        {
            int code = 0;
            bool isexitcar = false;
            Pagination pagination2 = new Pagination();
            pagination2.sord = "F_CreatorTime desc";
            pagination2.sidx = "F_CreatorTime";
            pagination2.page = 1;
            pagination2.rows = 1000000000;
            for (int i = 0; i < objlist.Count; i++)
            {
                var carriageid = objlist[i].InitBelongCarriageId;
                var carBoxList = service.FindList(z => z.InitBelongCarriageId == carriageid, pagination2);
                if (carBoxList != null && carBoxList.Count > 0)
                {
                    carBoxList.ForEach(f =>
                    {
                        service.Delete(f);
                    });
                    //service.Delete(carBox);
                    //msg += string.Format("{0},此车号已经存在！", objlist[i].CarriageBoxID);
                    //isexitcar = true;
                }
            }
            if (isexitcar)
            {
                code = -3;
            }
            else
            {
                List<CarriageBoxEntity> carriageBoxList = new List<CarriageBoxEntity>();
                for (int i = 0; i < objlist.Count; i++)
                {
                    var temp = new CarriageBoxEntity();
                    temp.Create();
                    temp.CarriageBoxID = objlist[i].CarriageBoxID;
                    temp.InitBelongTime = DateTime.Now;
                    temp.BoxType = objlist[i].BoxType;
                    temp.InitBelongCarriageId = objlist[i].InitBelongCarriageId;
                    temp.ThingsName = objlist[i].ThingsName;
                    temp.FenHao = objlist[i].FenHao;
                    temp.Tarpaulin = objlist[i].Tarpaulin;
                    temp.Remark = objlist[i].Remark;
                    carriageBoxList.Add(temp);
                }
                code = service.insertCarriageBoxList(carriageBoxList);
                if (code >= 0)
                {
                    msg = "添加车辆箱号成功";
                }
                else
                {
                    msg = "添加车辆失败，请稍后重试";
                }
            }
            return code;
        }

        public List<CarriageBoxEntity> GetExportList(Pagination pagination, string keyword, string startTime, string endTime,string Daozhan,string Zhuangche,string Xiangzhu)
        {
            var expression = ExtLinq.True<CarriageBoxEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(f => f.CarriageBoxID == keyword);
            }
            if (!string.IsNullOrEmpty(Daozhan))
            {
                expression = expression.And(f => f.InitBelongCarriageId.Contains(Daozhan));
            }
            if (!string.IsNullOrEmpty(Zhuangche))
            {
                expression = expression.And(f => f.ChangeBelongCarriageId.Contains(Zhuangche));
            }
            if (!string.IsNullOrEmpty(Xiangzhu))
            {
                expression = expression.And(f => f.BoxType.Equals(Xiangzhu));
            }
            if (!string.IsNullOrEmpty(startTime))
            {
                DateTime sTime = Convert.ToDateTime(startTime);
                if (!string.IsNullOrEmpty(endTime))
                {
                    DateTime eTime = Convert.ToDateTime(endTime);
                    expression = expression.And(f => f.LeaveTime >= sTime);
                    expression = expression.And(f => f.LeaveTime < eTime);
                }
                else
                {
                    expression = expression.And(f => f.LeaveTime >= sTime);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(endTime))
                {
                    DateTime eTime = Convert.ToDateTime(endTime);
                    expression = expression.And(f => f.LeaveTime < eTime);
                }

            }
            return service.FindList(expression, pagination);
        }

        /// <summary>
        /// 通过车辆信息查询它自己的箱号信息
        /// </summary>
        /// <param name="belongCarriage"></param>
        /// <returns></returns>
        public string GetCarBoxCodes(CarriageEntity belongCarriage,ref string tempFenhao)
        {
            string result = "";
           string Fenhao = "";
            Pagination pagination2 = new Pagination();
            pagination2.sord = "F_CreatorTime desc";
            pagination2.sidx = "F_CreatorTime";
            pagination2.page = 1;
            pagination2.rows = 1000000000;
            if (belongCarriage != null)
            {
                List<CarriageBoxEntity> carriageBoxList =
                    service.FindList(f => f.ChangeBelongCarriageId == belongCarriage.CarriageID, pagination2);
                if (carriageBoxList != null && carriageBoxList.Count > 0)
                {
                    carriageBoxList.ForEach(f =>
                    { 
                        result += f.CarriageBoxID + "/";
                        Fenhao += f.FenHao + "/";
                    });
                    result = result.Substring(0, result.Length - 1);
                    Fenhao = Fenhao.Substring(0, Fenhao.Length - 1);
                    tempFenhao = Fenhao;
                }
            }
            return result;
        }
    }

}
